package day12_thread_code.com.itheima.d6_thread_synchronized_lock;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Account {
    private String cardId;
    private double money;
    private final Lock lock = new ReentrantLock();

    public void drawMoney(double drawMoney){
        String name = Thread.currentThread().getName();
        lock.lock();
        if (this.money >= drawMoney){
            System.out.println(name + "取钱成功，取钱：" + drawMoney);
            this.money -= drawMoney;

            System.out.println(name + "取钱成功，余额：" + this.money);
        }else {
            System.out.println(name + "取钱失败，余额不足！");
        }
        lock.unlock();
    }
}
